package com.czy.enchantmentvisualization.controller;

import com.czy.enchantmentvisualization.common.GeneralInformation;
import com.czy.enchantmentvisualization.common.R;
import com.czy.enchantmentvisualization.domain.entity.Type;
import com.czy.enchantmentvisualization.emnu.RaritySort;
import com.czy.enchantmentvisualization.domain.bo.EnchantmentBo;
import com.czy.enchantmentvisualization.domain.bo.SortBo;
import com.czy.enchantmentvisualization.domain.vo.EnchantmentVo;
import com.czy.enchantmentvisualization.emnu.Sort;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/enchantment")
public class EnchantmentController {

    /**
     * 获取附魔列表
     *
     * @param enchantmentBo
     * @return
     */
    @PostMapping("/list")
    public R<List<EnchantmentVo>> list(@RequestBody EnchantmentBo enchantmentBo) {
        List<EnchantmentVo> list = GeneralInformation.enchantmentVos.stream()
                .filter(item -> !item.getBool())
                .filter(item -> !StringUtils.hasText(enchantmentBo.getDisplayName()) || item.getDisplayName().contains(enchantmentBo.getDisplayName()))
                .filter(item -> !StringUtils.hasText(enchantmentBo.getType()) || item.getType().getId().contains(enchantmentBo.getType()))
                .filter(item -> ObjectUtils.isEmpty(enchantmentBo.getTargets()) || item.getTargets().containsAll(enchantmentBo.getTargets()))
                .filter(item -> !StringUtils.hasText(enchantmentBo.getRarity()) || item.getRarity().getDisplayName().contains(enchantmentBo.getRarity()))
                .filter(item -> ObjectUtils.isEmpty(enchantmentBo.getTradeable()) || item.getTradeable().equals(enchantmentBo.getTradeable()))
                .filter(item -> ObjectUtils.isEmpty(enchantmentBo.getDiscoverable()) || item.getDiscoverable().equals(enchantmentBo.getDiscoverable()))
                .filter(item -> ObjectUtils.isEmpty(enchantmentBo.getEnchantable()) || item.getEnchantable().equals(enchantmentBo.getEnchantable()))
                .collect(Collectors.toList());

        sort(list, enchantmentBo.getSortBo());

        return R.ok(list);
    }

    private void sort(List<EnchantmentVo> list, SortBo sortBo) {
        if (ObjectUtils.isEmpty(sortBo)) return;
        if (ObjectUtils.isEmpty(sortBo.getRaritySort())) return;

        if (sortBo.getRaritySort().equals(Sort.RARITY_SORT_ASC.getCode())) {
            list.sort((o1, o2) -> getRaritySort(o1.getRarity().getDisplayName()).compareTo(getRaritySort(o2.getRarity().getDisplayName())));
        }

        if (sortBo.getRaritySort().equals(Sort.RARITY_SORT_DESC.getCode())) {
            list.sort((o1, o2) -> getRaritySort(o2.getRarity().getDisplayName()).compareTo(getRaritySort(o1.getRarity().getDisplayName())));
        }

        list.sort(Comparator.comparing((EnchantmentVo o) -> {
            if (o.getType() != null && "curse".equals(o.getType().getId())) {
                return 1; // 返回1表示o排在后面
            } else {
                return 0; // 返回0表示o的顺序不变
            }
        }));
    }


    private Integer getRaritySort(String rarity) {
        switch (rarity) {
            case "普通":
                return RaritySort.RARITY_COMMON.getSort();
            case "不寻常":
                return RaritySort.RARITY_UNCOMMON.getSort();
            case "稀有":
                return RaritySort.RARITY_RARE.getSort();
            case "史诗":
                return RaritySort.RARITY_EPIC.getSort();
            case "传奇":
                return RaritySort.RARITY_LEGENDARY.getSort();
            case "特殊":
                return RaritySort.RARITY_SPECIAL.getSort();
            case "非常特殊":
                return RaritySort.RARITY_VERYSPECIAL.getSort();
            default:
                return null; // 处理未知的稀有度
        }
    }

}
